package test.netty.server;

import fox.framework.rpc.common.WebUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.ByteProcessor;

import java.util.List;

/**
 * @author cuichao
 * @Description: ClientDecode
 * @Date: create in 2020/12/21 14:51
 */
public class ServerDecode extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("protocol:-----------------------------------------------------------");
        int i = in.forEachByte(ByteProcessor.FIND_LF);
        if(i <= 0){
            ctx.fireChannelRead(in);
            return;
        }
        byte[] protocolBuffer = new byte[i+1];
        in.readBytes(protocolBuffer);
        String protocolBlock  = new String(protocolBuffer,0 , protocolBuffer.length -1);
        //以空格为间隔
        String[] protocols = protocolBlock.split(" ");
        int len = protocols.length;
        if(len < 2){
            in.resetReaderIndex();
            ctx.fireChannelRead(in);
            return;
        }

        String protocol = protocols[0];
        String path = protocols[1];
        System.out.println(String.format("protocol: %s",protocol));
        System.out.println(String.format("path: %s",path));

        System.out.println("header:-----------------------------------------------------------");
        int i1 = in.forEachByte(i+1, in.readableBytes(), ByteProcessor.FIND_LF);
        byte[] headerBuffer = new byte[i1-i];
        in.readBytes(headerBuffer);
        String headerBlock = new String(headerBuffer,0,headerBuffer.length -1 );
        //每个entry以;分号间隔
        String[] entries = headerBlock.split(";");
        //每个k,v以:间隔

        for (String entry : entries) {
            System.out.println(entry);
            String[] split = entry.split(":");
            for (String s : split) {
                System.out.println(WebUtils.urlDecode(s));
            }
        }
        System.out.println("body:-----------------------------------------------------------");
        int contentLength = 13;


        if(in.readableBytes() < contentLength){
            System.err.println("body 长度不对");
            in.resetReaderIndex();
            return;
        }

        byte[] body = new byte[contentLength];
        in.readBytes(body);
        System.out.println("body"+new String(body));
        System.out.println("readableBytes:"+in.readableBytes());
        System.out.println("--------------------------------------------------------------------------");





        /*int k = in.readableBytes();

        System.err.println(k);

        if(k >= 8){
            int i = in.readInt();
            byte[] b = new byte[i];;
            in.readBytes(b);
            String result = new String(b,CharsetUtil.UTF_8);
            out.add(result);
        }*/

    }


    @Override
    protected void decodeLast(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.err.println("last");
    }
}
